import { defineStore } from 'pinia'
import {
    userLoginAPI,
    updateUserUnameAPI,
    updateUserPwdAPI,
    updateUserTelephoneAPI,
    updateUserAddressAPI,
    userRegisterAPI
} from '@/apis/user'
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
export const useUserStore = defineStore('user', () => {


    const userInfo = ref({
        code: 0,
        data: {
            uname: "",
            address: "",
            telephone: ""
        }
    })
    // 验证账号密码，获取用户基本信息,如果验证错误则返回code返回0
    const userLogin = async (account, password) => {
        const res = await userLoginAPI(account, password);
        userInfo.value = res;
    }
    //用户注册
    const userRegister = async (uid, uname, password) => {
        const res = await userRegisterAPI(uid, uname, password)
        return res.code;
    }
    //修改用户密码
    const updateUserPwd = async (uid, oldpwd, newpwd) => {
        const res = await updateUserPwdAPI(uid, oldpwd, newpwd);
        return res.code;
    }

    //修改用户呢称
    const updateUserUname = async (id, name) => {
        await updateUserUnameAPI(id, name);
        userInfo.value.data.uname = name;
    }
    //修改用户电话
    const updateUserTelephone = async (uid, telephone) => {
        await updateUserTelephoneAPI(uid, telephone);
        userInfo.value.data.telephone = telephone;
    }
    //修改用户地址
    const updateUserAddress = async (uid, address) => {
        await updateUserAddressAPI(uid, address)
        userInfo.value.data.address = address;
    }



    return {
        userInfo,
        userLogin,
        userRegister,
        updateUserUname,
        updateUserPwd,
        updateUserTelephone,
        updateUserAddress

    }
}, {
    persist: true
})